import { Component, OnDestroy } from '@angular/core';

import { ShopBrandService } from './shop.brand.service';

import { Util } from '../../../utils/util';

@Component({
  selector: 'shop-brand',
  templateUrl: './shop.brand.html',
  providers: [ShopBrandService]
})
export class ShopBrandComponent {

  shopInfo: any;
  p = 1;
  rows = 10;
  constructor(private service: ShopBrandService, private util: Util) {

    this.shopInfo = this.util.sessionStore().get('shopDetail');

    console.log(this.shopInfo);

    this.getHttp(this.shopInfo);

    this.vm.column.fileName = this.shopInfo.shopName + "品牌销售数据";
  }


  vm = {
    "loading": true,
    "data": [],
    "column": {
      fileName: '',
      dim: ['id', 'date', 'uid', 'brandName', 'sellAmount', 'sellMoney', 'monthSellAmount', 'monthSellMoney', 'productNum'],
      dimText: ['id', '日期', '店铺id', '品牌', '日销售量', '日销售额', '近30天销售量', '近30天销售额', '动销宝贝数']
    },
    "type": 'pie-brand'
  };

  // 参数
  param: any = {
    sort: 'sellMoney',
    sortType: 'desc'
  };

  allBrandsList: any = [];

  // 品牌
  columns: any = [
    {
      title: '日期',
      name: 'date',
      sort: false
    },
    {
      title: '品牌名称',
      name: 'brandName',
      sort: false
    },
    {
      title: "日销售量",
      name: 'sellAmount',
      sort: true
    },
    {
      title: '日销售额',
      name: 'sellMoney',
      sort: true
    },
    {
      title: '近30天销售量',
      name: 'monthSellAmount',
      sort: true
    },
    {
      title: '近30天销售额',
      name: 'monthSellMoney',
      sort: true
    },
    {
      title: '动销宝贝数',
      name: 'productNum',
      sort: true
    }
  ];

  // st-sort directive emit
  sortChanged(sort) {
    console.log('xxx');
    console.log(sort);
    this.getTableHttp(sort);
  }

  ngOnDestroy() {
    if (this.offEvent) {
      this.offEvent.unsubscribe();
    }
    if (this.offEvent2) {
      this.offEvent2.unsubscribe();
    }
    if (this.offEvent3) {
      this.offEvent3.unsubscribe();
    }

  }

  offEvent: any;
  getHttp(_shop) {

    var res = {"code":0,"msg":"成功","data":[{"id":"725677994\u0001three squirrels/三只松鼠","date":"2017-12-03 00:00:00","uid":725677994,"brandName":"three squirrels/三只松鼠","sellAmount":178361,"monthSellAmount":5441532,"sellMoney":4314915.6,"productNum":234,"monthSellMoney":1.27501689E8},{"id":"725677994\u0001vinda/维达","date":"2017-12-03 00:00:00","uid":725677994,"brandName":"vinda/维达","sellAmount":79470,"monthSellAmount":3311869,"sellMoney":2062570.0,"productNum":164,"monthSellMoney":1.071405309E8},{"id":"725677994\u0001蒙牛","date":"2017-12-03 00:00:00","uid":725677994,"brandName":"蒙牛","sellAmount":33869,"monthSellAmount":1862192,"sellMoney":1421753.2,"productNum":77,"monthSellMoney":8.88627896E7},{"id":"725677994\u0001be＆cheery/百草味","date":"2017-12-03 00:00:00","uid":725677994,"brandName":"be＆cheery/百草味","sellAmount":162487,"monthSellAmount":4634610,"sellMoney":3044605.06,"productNum":217,"monthSellMoney":8.858020684E7},{"id":"725677994\u0001清风","date":"2017-12-03 00:00:00","uid":725677994,"brandName":"清风","sellAmount":58205,"monthSellAmount":2127525,"sellMoney":2132895.2,"productNum":145,"monthSellMoney":8.18194767E7},{"id":"725677994\u0001良品铺子","date":"2017-12-03 00:00:00","uid":725677994,"brandName":"良品铺子","sellAmount":113177,"monthSellAmount":3660028,"sellMoney":2019240.68,"productNum":268,"monthSellMoney":6.747942034E7},{"id":"725677994\u0001蓝月亮","date":"2017-12-03 00:00:00","uid":725677994,"brandName":"蓝月亮","sellAmount":41653,"monthSellAmount":1718361,"sellMoney":1523045.9,"productNum":188,"monthSellMoney":5.82672619E7},{"id":"725677994\u0001伊利","date":"2017-12-03 00:00:00","uid":725677994,"brandName":"伊利","sellAmount":18404,"monthSellAmount":1236262,"sellMoney":769289.12,"productNum":98,"monthSellMoney":5.422847716E7},{"id":"725677994\u0001金龙鱼","date":"2017-12-03 00:00:00","uid":725677994,"brandName":"金龙鱼","sellAmount":35830,"monthSellAmount":1165342,"sellMoney":1606921.48,"productNum":156,"monthSellMoney":5.133984186E7},{"id":"725677994\u0001花王/妙而舒","date":"2017-12-03 00:00:00","uid":725677994,"brandName":"花王/妙而舒","sellAmount":13858,"monthSellAmount":484618,"sellMoney":1295257.0,"productNum":31,"monthSellMoney":4.9949394E7},{"id":"725677994\u0001walch/威露士","date":"2017-12-03 00:00:00","uid":725677994,"brandName":"walch/威露士","sellAmount":38298,"monthSellAmount":975061,"sellMoney":1387606.15,"productNum":164,"monthSellMoney":4.899265365E7},{"id":"725677994\u0001clear/清扬","date":"2017-12-03 00:00:00","uid":725677994,"brandName":"clear/清扬","sellAmount":4364,"monthSellAmount":328127,"sellMoney":335969.0,"productNum":60,"monthSellMoney":4.39208821E7},{"id":"725677994\u0001pampers/帮宝适","date":"2017-12-03 00:00:00","uid":725677994,"brandName":"pampers/帮宝适","sellAmount":7633,"monthSellAmount":264795,"sellMoney":1180218.7,"productNum":39,"monthSellMoney":4.14362914E7},{"id":"725677994\u0001福临门","date":"2017-12-03 00:00:00","uid":725677994,"brandName":"福临门","sellAmount":17102,"monthSellAmount":759992,"sellMoney":745175.4,"productNum":94,"monthSellMoney":3.92921747E7},{"id":"725677994\u0001心相印","date":"2017-12-03 00:00:00","uid":725677994,"brandName":"心相印","sellAmount":29918,"monthSellAmount":1087994,"sellMoney":812540.74,"productNum":114,"monthSellMoney":3.865785172E7},{"id":"725677994\u0001海飞丝","date":"2017-12-03 00:00:00","uid":725677994,"brandName":"海飞丝","sellAmount":8235,"monthSellAmount":413656,"sellMoney":620106.6,"productNum":62,"monthSellMoney":3.3263638E7},{"id":"725677994\u0001奥妙","date":"2017-12-03 00:00:00","uid":725677994,"brandName":"奥妙","sellAmount":9474,"monthSellAmount":635876,"sellMoney":381188.1,"productNum":70,"monthSellMoney":3.25336912E7},{"id":"725677994\u0001鲁花","date":"2017-12-03 00:00:00","uid":725677994,"brandName":"鲁花","sellAmount":6033,"monthSellAmount":260587,"sellMoney":620971.6,"productNum":26,"monthSellMoney":3.01033881E7},{"id":"725677994\u0001moutai/茅台","date":"2017-12-03 00:00:00","uid":725677994,"brandName":"moutai/茅台","sellAmount":414,"monthSellAmount":24921,"sellMoney":87771.6,"productNum":24,"monthSellMoney":2.89673824E7},{"id":"725677994\u0001nestle/雀巢","date":"2017-12-03 00:00:00","uid":725677994,"brandName":"nestle/雀巢","sellAmount":16248,"monthSellAmount":583613,"sellMoney":621045.54,"productNum":191,"monthSellMoney":2.600972954E7},{"id":"725677994\u0001洁柔","date":"2017-12-03 00:00:00","uid":725677994,"brandName":"洁柔","sellAmount":14542,"monthSellAmount":616275,"sellMoney":534910.7,"productNum":96,"monthSellMoney":2.47951071E7},{"id":"725677994\u0001sofy/苏菲","date":"2017-12-03 00:00:00","uid":725677994,"brandName":"sofy/苏菲","sellAmount":26108,"monthSellAmount":1007971,"sellMoney":463928.8,"productNum":87,"monthSellMoney":2.38048081E7},{"id":"725677994\u0001huggies/好奇","date":"2017-12-03 00:00:00","uid":725677994,"brandName":"huggies/好奇","sellAmount":4616,"monthSellAmount":193718,"sellMoney":558190.0,"productNum":41,"monthSellMoney":2.35818192E7},{"id":"725677994\u0001whisper/护舒宝","date":"2017-12-03 00:00:00","uid":725677994,"brandName":"whisper/护舒宝","sellAmount":15065,"monthSellAmount":544052,"sellMoney":410344.3,"productNum":96,"monthSellMoney":2.270520679E7},{"id":"725677994\u0001光明","date":"2017-12-03 00:00:00","uid":725677994,"brandName":"光明","sellAmount":9773,"monthSellAmount":333303,"sellMoney":588475.8,"productNum":34,"monthSellMoney":2.19561317E7},{"id":"725677994\u0001云南白药","date":"2017-12-03 00:00:00","uid":725677994,"brandName":"云南白药","sellAmount":7576,"monthSellAmount":454757,"sellMoney":229438.8,"productNum":55,"monthSellMoney":1.96597736E7},{"id":"725677994\u0001dove/多芬","date":"2017-12-03 00:00:00","uid":725677994,"brandName":"dove/多芬","sellAmount":8466,"monthSellAmount":411325,"sellMoney":458006.3,"productNum":117,"monthSellMoney":1.956249E7},{"id":"725677994\u0001十月稻田","date":"2017-12-03 00:00:00","uid":725677994,"brandName":"十月稻田","sellAmount":13520,"monthSellAmount":496831,"sellMoney":491539.4,"productNum":66,"monthSellMoney":1.92143485E7},{"id":"725677994\u0001mighty/多力","date":"2017-12-03 00:00:00","uid":725677994,"brandName":"mighty/多力","sellAmount":3582,"monthSellAmount":248713,"sellMoney":277714.6,"productNum":28,"monthSellMoney":1.87816729E7},{"id":"725677994\u0001pechoin/百雀羚","date":"2017-12-03 00:00:00","uid":725677994,"brandName":"pechoin/百雀羚","sellAmount":6295,"monthSellAmount":282247,"sellMoney":375314.4,"productNum":125,"monthSellMoney":1.85532262E7},{"id":"725677994\u0001tide/汰渍","date":"2017-12-03 00:00:00","uid":725677994,"brandName":"tide/汰渍","sellAmount":10440,"monthSellAmount":343238,"sellMoney":507498.56,"productNum":59,"monthSellMoney":1.838483635E7},{"id":"725677994\u0001金纺","date":"2017-12-03 00:00:00","uid":725677994,"brandName":"金纺","sellAmount":10438,"monthSellAmount":411704,"sellMoney":506089.2,"productNum":33,"monthSellMoney":1.8266175E7},{"id":"725677994\u0001wyeth/惠氏","date":"2017-12-03 00:00:00","uid":725677994,"brandName":"wyeth/惠氏","sellAmount":365,"monthSellAmount":62679,"sellMoney":88678.0,"productNum":34,"monthSellMoney":1.8022688E7},{"id":"725677994\u0001立白","date":"2017-12-03 00:00:00","uid":725677994,"brandName":"立白","sellAmount":26458,"monthSellAmount":776568,"sellMoney":624110.4,"productNum":96,"monthSellMoney":1.76543732E7},{"id":"725677994\u0001德运","date":"2017-12-03 00:00:00","uid":725677994,"brandName":"德运","sellAmount":4110,"monthSellAmount":228255,"sellMoney":285841.8,"productNum":10,"monthSellMoney":1.56428659E7},{"id":"725677994\u0001schwarzkopf/施华蔻","date":"2017-12-03 00:00:00","uid":725677994,"brandName":"schwarzkopf/施华蔻","sellAmount":3829,"monthSellAmount":209349,"sellMoney":258250.9,"productNum":119,"monthSellMoney":1.55774215E7},{"id":"725677994\u0001潘婷","date":"2017-12-03 00:00:00","uid":725677994,"brandName":"潘婷","sellAmount":6556,"monthSellAmount":332491,"sellMoney":303898.28,"productNum":33,"monthSellMoney":1.549977802E7},{"id":"725677994\u0001妈咪宝贝","date":"2017-12-03 00:00:00","uid":725677994,"brandName":"妈咪宝贝","sellAmount":1433,"monthSellAmount":128334,"sellMoney":136928.2,"productNum":36,"monthSellMoney":1.49082756E7},{"id":"725677994\u0001lux/力士","date":"2017-12-03 00:00:00","uid":725677994,"brandName":"lux/力士","sellAmount":8205,"monthSellAmount":397565,"sellMoney":286153.5,"productNum":97,"monthSellMoney":1.4906768E7},{"id":"725677994\u0001seeyoung/滋源","date":"2017-12-03 00:00:00","uid":725677994,"brandName":"seeyoung/滋源","sellAmount":3144,"monthSellAmount":167454,"sellMoney":252864.6,"productNum":60,"monthSellMoney":1.42018838E7},{"id":"725677994\u0001darlie/黑人","date":"2017-12-03 00:00:00","uid":725677994,"brandName":"darlie/黑人","sellAmount":19144,"monthSellAmount":809718,"sellMoney":279422.12,"productNum":118,"monthSellMoney":1.404478866E7},{"id":"725677994\u0001safeguard/舒肤佳","date":"2017-12-03 00:00:00","uid":725677994,"brandName":"safeguard/舒肤佳","sellAmount":8826,"monthSellAmount":478141,"sellMoney":222866.2,"productNum":74,"monthSellMoney":1.40111322E7},{"id":"725677994\u0001haday/海天","date":"2017-12-03 00:00:00","uid":725677994,"brandName":"haday/海天","sellAmount":19581,"monthSellAmount":645262,"sellMoney":336312.6,"productNum":99,"monthSellMoney":1.39951049E7},{"id":"725677994\u0001lay’s/乐事","date":"2017-12-03 00:00:00","uid":725677994,"brandName":"lay’s/乐事","sellAmount":21223,"monthSellAmount":790582,"sellMoney":324108.6,"productNum":147,"monthSellMoney":1.37516218E7},{"id":"725677994\u0001达利园","date":"2017-12-03 00:00:00","uid":725677994,"brandName":"达利园","sellAmount":15027,"monthSellAmount":569238,"sellMoney":322031.64,"productNum":70,"monthSellMoney":1.371450018E7},{"id":"725677994\u0001沙宣","date":"2017-12-03 00:00:00","uid":725677994,"brandName":"沙宣","sellAmount":3495,"monthSellAmount":169005,"sellMoney":274973.1,"productNum":47,"monthSellMoney":1.36558911E7},{"id":"725677994\u0001dettol/滴露","date":"2017-12-03 00:00:00","uid":725677994,"brandName":"dettol/滴露","sellAmount":8790,"monthSellAmount":271303,"sellMoney":441381.62,"productNum":62,"monthSellMoney":1.311793485E7},{"id":"725677994\u0001泉林本色","date":"2017-12-03 00:00:00","uid":725677994,"brandName":"泉林本色","sellAmount":7896,"monthSellAmount":309713,"sellMoney":297127.76,"productNum":96,"monthSellMoney":1.287038834E7},{"id":"725677994\u0001friso/美素佳儿","date":"2017-12-03 00:00:00","uid":725677994,"brandName":"friso/美素佳儿","sellAmount":439,"monthSellAmount":66468,"sellMoney":76122.0,"productNum":12,"monthSellMoney":1.285999E7},{"id":"725677994\u0001moony","date":"2017-12-03 00:00:00","uid":725677994,"brandName":"moony","sellAmount":1148,"monthSellAmount":111624,"sellMoney":113617.1,"productNum":23,"monthSellMoney":1.25524259E7}]};

    setTimeout(() => {

      this.allBrandsList = res.data;

      this.vm.data = res.data;

      this.vm.loading = false;

    }, 100);
  }

  offEvent2: any;
  getTableHttp(sort) {

    let json = {
      uid: this.shopInfo.uid
    };

    Object.assign(json, sort);

    this.offEvent2 = this.service.get(json).subscribe((res: any) => {

      this.allBrandsList = res.data;


    }, (err: any) => { });
  }


  // 导出
  export() {



    let data = this.util.jsonToArrry(this.allBrandsList, this.vm.column.dim, this.vm.column.dimText);

    this.util.export(data, this.vm.column.fileName);


  }


  catNameList: any;
  offEvent3: any;
  getPieHttp(_shop) {


    this.offEvent3 = this.service.getShopCat({ uid: _shop.uid }).subscribe((res: any) => {
      this.catNameList = res.data;

    }, (err: any) => { });


  }

}
